﻿Imports System.IO

Public Class frmUpdateStandardCost
    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim frm As New frmItemList
        frm.IsMultiSelect = True
        frm.FilterStr = "ItemCode in (Select Code from OITT) and EvalSystem='S'"
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each row In frm.GridDetail.GetCheckedRows
                Dim newRow As Janus.Windows.GridEX.GridEXRow = GridDetail.AddItem()
                newRow.BeginEdit()
                newRow.Cells("ItemCode").Value = row.Cells("ItemCode").Value.ToString
                newRow.Cells("ItemName").Value = row.Cells("ItemName").Value.ToString
                newRow.Cells("CurrentCost").Value = row.Cells("AvgPrice").Value.ToString
                newRow.EndEdit()
            Next
        End If
    End Sub

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try

        
            Dim lErrCode As Integer
            Dim sErrMsg As String = ""
            Dim oitem As SAPbobsCOM.Items
            oitem = PublicVariable.oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oItems)

            For Each row In GridDetail.GetRows
                oitem.GetByKey(row.Cells("ItemCode").Value.ToString)
                oitem.AvgStdPrice = row.Cells("NewCost").Value
                lErrCode = oitem.Update()

                If lErrCode <> 0 Then
                    PublicVariable.oCompany.GetLastError(lErrCode, sErrMsg)
                    MessageBox.Show(sErrMsg)
                    Return

                End If
            Next
            MessageBox.Show("Update Completed!")
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Me.Cursor = Cursors.WaitCursor
        Dim fn As New Functions
        Dim str As String
        Dim dt As DataTable
        

        For i As Integer = 0 To GridDetail.RowCount - 1
            GridDetail.Row = i
            str = "select sum(T1.LastPurPrc*T0.Quantity) NewCost from ITT1 T0"
            str = str + " join OITM T1 on T0.Code=T1.ItemCode where T0.Father='"
            str = str + GridDetail.GetValue("ItemCode").ToString + "'"
            dt = fn.DoQueryReturnDT(str)
            If dt.Rows.Count > 0 Then
                GridDetail.CurrentRow.BeginEdit()
                GridDetail.CurrentRow.Cells("NewCost").Value = dt.Rows(0).Item("NewCost")
                GridDetail.CurrentRow.EndEdit()
            End If

            

        Next


        Me.Cursor = Cursors.Default
    End Sub

    Private Sub btnClose_Click(sender As System.Object, e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub
End Class
